﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace antyspam_filter
{
    class Filter
    {
        //DataClasses1DataContext db;
        public Filter()
        {
            //
        }
        public bool filterEmail(String content)
        {

            DataClasses1DataContext db = new DataClasses1DataContext();

            EmailParser email = new EmailParser(content);
            bool whitelistTest = false;
            bool blacklistTest = false;

            // IP
            foreach(EmailServer s in email.Header.Servers)
            {
                if(ipWhitelistFilter(s.Ip))whitelistTest=true;
            }
            if (whitelistTest) return true;
            //DOMAIN 
            foreach (EmailServer s in email.Header.Servers)
            {
                if (adressWhitelistFilter(s.Address)) whitelistTest = true;
            }
            if (whitelistTest) return true;
            //ADRESS
            if (addressWhitelistFilter(email.Header.EmailFrom)) return true;

            ////BLACKLIST


            //DOMAIN 
            foreach (EmailServer s in email.Header.Servers)
            {
                if (adresBlacklistFilter(s.Address)) blacklistTest = true;
            }
            if (blacklistTest) return false;


            // IP
            foreach(EmailServer s in email.Header.Servers)
            {
                if(ipBlacklistFilter(s.Ip))blacklistTest=true;
            }
            if(blacklistTest)return false;

            // ADRESS
            if (addressBlacklistFilter(email.Header.EmailFrom)) return false;


            //WORDS


            var words = from w in db.BlockedWords select w;

            foreach (BlockedWord w in words.ToList())
            {
                if (email.Body.Text.Contains(w.wordValue))
                {
                    return false;
                }
            }




            return true;
        }
        private bool ipBlacklistFilter(string ip)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();

            var items = from l in db.Lists
                        where (l.category == 0 && l.data.Contains(ip) && l.type == true)
                        select l;

            if (items.Count() > 0) return true;

            return false; //DB.isIPBlacklisted(ip);
        }
        private bool addressBlacklistFilter(string address)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();

            var items = from l in db.Lists
                        where (l.category == 2 && l.data.Contains(address) && l.type == true)
                        select l;

            if (items.Count() > 0) return true;
            return false;// DB.isEmailAddresBlacklisted(ip);
        }
        private bool ipWhitelistFilter(string ip)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            var items = from l in db.Lists
                        where (l.category == 0 && l.data.Contains(ip) && l.type == false)
                        select l;

            if (items.Count() > 0) return true;

            return false; //DB.isIPBlacklisted(ip);
        }
        private bool addressWhitelistFilter(string address)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            var items = from l in db.Lists
                        where (l.category == 2 && l.data.Contains(address) && l.type == false)
                        select l;

            if (items.Count() > 0) return true;

            return false;
        }



        private bool adressWhitelistFilter(string address)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            var items = from l in db.Lists
                        where (l.category == 1 && l.data.Contains(address) && l.type == false)
                        select l;

            if (items.Count() > 0) return true;

            return false;
        }
        private bool adresBlacklistFilter(string address)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            var items = from l in db.Lists
                        where (l.category == 1 && l.data.Contains(address) && l.type == true)
                        select l;

            if (items.Count() > 0) return true;

            return false;
        }


    }
}
